﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using AssemblyInfoEditor.VersionManagement;

namespace AssemblyInfoEditor.Project
{
    public class ProjectAssemblyInfo
    {
        public ProjectAssemblyInfo(EnvDTE.Project project)
        {
            this.Project = project;
            Version = new ManagedVersion();

            LoadSettings();
        }

        public EnvDTE.Project Project { get; set; }
        public string Title { get; set; }
        public string Product { get; set; }
        public string Company { get; set; }
        public string Copyright { get; set; }
        public string Trademark { get; set; }
        public ManagedVersion Version { get; set; } 
        public bool IsVersionManagementEnabled { get; set; }

        public void SaveSettings()
        {
            SetProjectVariable("AIMgrVersionManagementEnabled", IsVersionManagementEnabled.ToString());
            SetProjectVariable("AIMgrBuildIncrementStyle", Version.BuildIncrementStyle.ToString());
            SetProjectVariable("AIMgrRevisionIncrementStyle", Version.RevisionIncrementStyle.ToString());

            SetProjectProperty("Title", Title);
            SetProjectProperty("Product", Product);
            SetProjectProperty("Company", Company);
            SetProjectProperty("Copyright", Copyright);
            SetProjectProperty("Trademark", Trademark);
            SetProjectProperty("AssemblyVersion", Version.ToString());
            SetProjectProperty("AssemblyFileVersion", Version.ToString());
        }

        public void LoadSettings()
        {
            Title = (string)GetProjectProperty("Title").Value;
            Product = (string)GetProjectProperty("Product").Value;
            Company = (string)GetProjectProperty("Company").Value;
            Copyright = (string)GetProjectProperty("Copyright").Value;
            Trademark = (string)GetProjectProperty("Trademark").Value;

            if ((Project.Globals.get_VariableExists("AIMgrVersionManagementEnabled")) &&
                (Project.Globals.get_VariableExists("AIMgrBuildIncrementStyle")))
            {
                IsVersionManagementEnabled = Boolean.Parse(GetProjectVariable("AIMgrVersionManagementEnabled"));
                Version.BuildIncrementStyle = (IncrementStyle)Enum.Parse(typeof(IncrementStyle), GetProjectVariable("AIMgrBuildIncrementStyle"));
                Version.RevisionIncrementStyle = (IncrementStyle)Enum.Parse(typeof(IncrementStyle), GetProjectVariable("AIMgrRevisionIncrementStyle"));
            }
            else
            {
                Version = ManagedVersion.ConvertFromString((string)GetProjectProperty("AssemblyVersion").Value);

                IsVersionManagementEnabled = false;
            }
        }

        public Property GetProjectProperty(string PropertyName)
        {
            Property returnProp = null;

            foreach (Property prop in Project.Properties)
            {
                if (prop.Name == PropertyName)
                {
                    return prop;
                }
            }

            return returnProp;
        }

        public void SetProjectProperty(string PropertyName, string PropertyValue)
        {
            foreach (Property prop in Project.Properties)
            {
                if (prop.Name == PropertyName)
                {
                    prop.Value = PropertyValue;
                }
            }
        }

        public string GetProjectVariable(string VariableName)
        {
            string s = String.Empty;

            object[] names = (object[])Project.Globals.VariableNames;

            if (Project.Globals.get_VariableExists(VariableName))
            {
                foreach (object name in names)
                {
                    if (name.ToString() == VariableName)
                    {
                        return (string)Project.Globals[VariableName];
                    }
                }
            }

            return s;
        }

        public void SetProjectVariable(string VariableName, string Value)
        {
            Project.Globals[VariableName] = Value;
            Project.Globals.set_VariablePersists(VariableName, true);
        }
    }
}
